package com.example.util;

/**
 * @author LiuWei
 * @description 时间工具类
 * @date 2019/6/5
 */

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param timestamp
     * @param format    日期格式
     * @return
     */
    public static String timeStamp2Date(Long timestamp, String format) {
        if (timestamp == null || timestamp < 0) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(timestamp));
    }

    public static Date timeStamp2Date1(Long timestamp, String format) {
        if (timestamp == null || timestamp < 0) {
            return null;
        }
        if (format == null || format.isEmpty()) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return new Date(timestamp);
    }


    public static String timeStamp2Date(Long timestamp) {
        if (timestamp == null || timestamp < 0) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        return sdf.format(new Date(timestamp));
    }

    /**
     * 日期格式字符串转换成时间戳 ms
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return
     */
    public static String timeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    public static Date str2Date(String date_str, String format) {
        try {
            if (format == null) {
                format = DEFAULT_FORMAT;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(date_str);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param date
     * @param format    日期格式
     * @return
     */
    public static String date2Str(Date date, String format) {
        if (date == null) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Long getWholeTimeStamp(){
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY,0);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        instance.set(Calendar.MILLISECOND,0);
        return instance.getTimeInMillis();
    }


//    /**
//     * 获取相对时间
//     *
//     * @param recentTime     时间
//     * @param recentTimeUnit 时间单位
//     * @return Date
//     */
//    public static Date getRelativeDate(int recentTime, int recentTimeUnit, LocalDateTime dateTime) {
//
//        if (recentTimeUnit == 1) {
//            dateTime = dateTime.minusMinutes(recentTime);
//        } else if (recentTimeUnit == 2) {
//            dateTime = dateTime.minusHours(recentTime);
//        } else if (recentTimeUnit == 3) {
//            dateTime = dateTime.minusDays(recentTime);
//        } else {
//            return null;
//        }
//        return LocalDateTimeUtils.convertLDTToDate(dateTime);
//    }

    public static String stringUTCtoString(String utcTime){
        String UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
        Date utcDate = str2Date(utcTime, UTC_FORMAT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(utcDate);
        calendar.add(Calendar.HOUR, 8);
        Date date = calendar.getTime();
        return date2Str(date, DEFAULT_FORMAT);
    }

}
